<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <a href="#/home">首页</a> ---
    <a href="#/list">列表</a> ---
    <a href="#/my">我的</a>


    <div id="box">

    </div>


    <script>
        let box = document.getElementById('box')

        const routes = [
            {
                path: "/home",
                component: "我是Home中的内容"
            },
            {
                path: "/list",
                component: "我是List中的内容"
            },
            {
                path: "/my",
                component: "我是My中的内容"
            }
        ]

        // hashchange   url  #后的内容发生改变
        window.addEventListener("hashchange", () => {
            // 拿到#后面的值
            const url = location.hash.slice(1)
            console.log(url)
            // changeView(url)
            // arr.find   找到当前这一项
            // findIndex  找到当前匹配到的下标
            const index = routes.findIndex(item => item.path === url)
            box.innerHTML = routes[index].component
        })


        



        // function changeView(url) {
        //     switch (url) {
        //         case "/home":
        //             box.innerHTML = "我是首页"
        //             break
        //         case "/list":
        //             box.innerHTML = "我是列表"
        //             break
        //         case "/my":
        //             box.innerHTML = "我的页面"
        //             break
        //     }
        // }


    </script>
</body>

</html>